package com.xjj.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjj.entity.ResponseEntity;
import com.xjj.entity.School;
import com.xjj.excel.ExcelImport;
import com.xjj.excel.ExcelUtils;
import com.xjj.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sxy
 * @version 1.0
 * @date 2022/10/20 11:27
 */
@RestController
@Api(value = "该类是学校的相关模块")
@RequestMapping("/manage/admin/school")
public class SchoolController {
    @Resource
    private SchoolService schoolService;

    @PostMapping("/addSchool")
    @ApiOperation(value = "添加学校")
    public ResponseEntity addSchool(@RequestBody School school){
        boolean save = schoolService.save(school);
        return ResponseEntity.SUCCESS("添加学校成功",200);
    }

    @DeleteMapping("/deleteSchoolById/{id}")
    @ApiOperation(value = "通过id删除学校")
    public ResponseEntity deleteSchoolById(@PathVariable Integer id){
        boolean flag = schoolService.removeById(id);
        return new ResponseEntity<>(200,"ok",flag);
    }

    @ApiOperation(value = "查找所有学校所有信息")
    @GetMapping("/findAllSchool")
    public ResponseEntity findAllSchool(){
        List<School> schools = schoolService.list();
        return new ResponseEntity<>(200,"ok",schools);
    }

    @GetMapping("/getAllSchool/{current}")
    @ApiOperation(value = "分页查询所有学校")
    public ResponseEntity getAllSchoolPage(@PathVariable("current") Integer current){
        if(current==0){
            Page<School> school = schoolService.getSchool(1);
            return new ResponseEntity(200,"获取分页查询学校信息",school);
        }else {
            Page<School> school = schoolService.getSchool(current);
            return new ResponseEntity(200,"获取分页查询学校信息",school);
        }
    }

    @PostMapping("/import")
    @ApiOperation(value = "excel导入学校信息")
    public ResponseEntity importSchool(@RequestPart("file") MultipartFile file)throws Exception{
        List<School> schools = ExcelUtils.readMultipartFile(file,School.class);
        for (School school : schools) {
            schoolService.save(school);
            System.out.println(school);
        }
        return ResponseEntity.SUCCESS("导入成功",200);
    }

    @PutMapping("/updateSchoolById")
    @ApiOperation(value = "通过id修改学校信息")
    public ResponseEntity updateSchoolById(@RequestBody School school){
        schoolService.updateById(school);
        return ResponseEntity.SUCCESS("ok",200);
    }


}
